package mysql

import "pharymacy-management-system/models"

func SelectStatusByStatusName(statusName string) (*models.SalesStatus, error) {
	var salesStatus models.SalesStatus
	sqlStr := `select status_id, status_name from w_status where status_name=?`
	err := db.Get(&salesStatus, sqlStr, statusName)
	if err != nil {
		return nil, err
	}
	return &salesStatus, nil
}

func SelectStatusByStatusID(statusID int) (*models.SalesStatus, error) {
	var salesStatus models.SalesStatus
	sqlStr := `select status_id, status_name from w_status where status_id=?`
	err := db.Get(&salesStatus, sqlStr, statusID)
	if err != nil {
		return nil, err
	}
	return &salesStatus, nil
}

func InsertStatus(statusName string) error {
	sqlStr := `insert into w_status(status_name) value (?)`
	_, err := db.Exec(sqlStr, statusName)
	return err
}

func UpdateStatus(id int, name string) error {
	sqlStr := `update w_status set status_name=? where status_id=?`
	_, err := db.Exec(sqlStr, name, id)
	return err
}

func SelectAllStatus() (statusList []models.SalesStatus, err error) {
	sqlStr := `select status_id, status_name from w_status`
	err = db.Select(&statusList, sqlStr)
	return statusList, err
}

func CreateOrder(sale *models.Sales) error {
	sqlStr := `insert into w_order(order_id,status_id,create_user_id) value (?,?,?)`
	_, err := db.Exec(sqlStr, sale.OrderID, sale.StatusID, sale.CreateUserID)
	return err
}

func SelectOrderByID(ID int64) (*models.Sales, error) {
	var order models.Sales
	sqlStr := `select order_id, create_user_id, approve_user_id, exec_user_id, settlement_user_id, a.create_time, a.update_time, status_id,b.user_nick_name as create_user_name from w_order a,w_user b where a.create_user_id=b.user_id and order_id=?`
	err := db.Get(&order, sqlStr, ID)
	if order.ApproveUserID != 0 {
		sqlStr = `select user_nick_name from w_user where user_id=?`
		var userNickName string
		err := db.Get(&userNickName, sqlStr, order.ApproveUserID)
		if err != nil {
			return nil, err
		}
		order.ApproveUserName = userNickName
		if order.ExecUserID != 0 {
			sqlStr = `select user_nick_name from w_user where user_id=?`
			var userNickName string
			err := db.Get(&userNickName, sqlStr, order.ExecUserID)
			if err != nil {
				return nil, err
			}
			order.ExecUserName = userNickName
			if order.SettlementUserID != 0 {
				sqlStr = `select user_nick_name from w_user where user_id=?`
				var userNickName string
				err := db.Get(&userNickName, sqlStr, order.SettlementUserID)
				if err != nil {
					return nil, err
				}
				order.SettlementUserName = userNickName

			}
		}
	}
	sqlStr = `select w_status.status_name from w_status where status_id=?`
	var statusName string
	err = db.Get(&statusName, sqlStr, order.StatusID)
	if err != nil {
		return nil, err
	}
	order.SalesStatus.StatusName = statusName
	return &order, err
}

func UpdateOrderByID(sales *models.Sales) error {
	sqlStr := `update w_order set status_id=?,approve_user_id=?,exec_user_id=?,settlement_user_id=? where order_id=?`
	_, err := db.Exec(sqlStr, sales.StatusID, sales.ApproveUserID, sales.ExecUserID, sales.SettlementUserID, sales.OrderID)
	return err
}

func SelectOrderListByStatusID(statusID int) (orderList []models.Sales, err error) {
	sqlStr := `select a.order_id,
       a.create_user_id,
       a.approve_user_id,
       a.exec_user_id,
       a.settlement_user_id,
       a.create_time,
       a.update_time,
       a.status_id,
       b.status_name
from w_order a,
     w_status b
where a.status_id = b.status_id
  and a.status_id = ?`
	rows, err := db.Queryx(sqlStr, statusID)
	if err != nil {
		return
	}
	defer rows.Close()

	for rows.Next() {
		var order models.Sales
		err := rows.Scan(&order.OrderID, &order.CreateUserID, &order.ApproveUserID, &order.ExecUserID, &order.SettlementUserID, &order.CreateTime, &order.UpdateTime, &order.StatusID, &order.StatusName)
		if err != nil {
			return nil, err
		}
		orderList = append(orderList, order)
	}
	return
}
